package com.lm.scau.framedemos.Views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Function:
 * auth：liming on 2016/7/13 11:05
 * mail：liming@wxchina.com
 */
public class CustomLineView extends View {
    //坐标原点坐标
    private int xPoint = 70;
    private int yPoint = 500;
    //坐标长度
    private int xLength = 600;
    private int yLength = 440;
    //刻度长度
    private int xScale = 20;
    private int yScale = 50;

    //坐标上能够绘制的点数
    private int xPointSize = xLength / xScale;
    private int yPointSize = yLength / yScale;
    private List<Integer> yPoints = new ArrayList<Integer>();   //存放 纵坐标 所描绘的点
    private Thread thread;

    private String[] yLable = new String[yLength / yScale];

    public Thread getThread() {
        return thread;
    }

    public CustomLineView(Context context) {
        super(context);
        initData();
    }

    public CustomLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    private void initData() {
        for (int i = 0; i < yLable.length; i++) {
            yLable[i] = (i + 1) + "M/s";
        }
        thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                        if (yPoints.size() > xPointSize) {
                            yPoints.remove(0);
                        }
                        yPoints.add(new Random().nextInt(yPointSize) + 1);  //生成1-6的随机数
                        mHander.sendEmptyMessage(0);   //发送空消息通知刷新
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }

    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (0 == msg.what) {
                CustomLineView.this.invalidate();  //重新绘制
            }
        }
    };

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        Paint textPaint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2);
        textPaint.setColor(Color.RED);
        textPaint.setTextSize(18);
        textPaint.setStrokeWidth(1.5f);
        //绘制x轴
        canvas.drawLine(xPoint, yPoint, xLength + xPoint, yPoint, paint);
        canvas.drawLine(xLength + xPoint, yPoint, xLength + xPoint - 5, yPoint - 5, paint);
        canvas.drawLine(xLength + xPoint, yPoint, xLength + xPoint - 5, yPoint + 5, paint);
        //绘制y轴
        canvas.drawLine(xPoint, yPoint, xPoint, yPoint - yLength, paint);
        canvas.drawLine(xPoint, yPoint - yLength, xPoint - 5, yPoint - yLength + 5, paint);
        canvas.drawLine(xPoint, yPoint - yLength, xPoint + 5, yPoint - yLength + 5, paint);
        //y轴上的刻度
        for (int i = 0; i < yPointSize; i++) {
            canvas.drawLine(xPoint, yPoint - (yScale * (i + 1)), xPoint + 8, yPoint - (yScale * (i + 1)), paint);
            canvas.drawText(yLable[i], xPoint - 48, yPoint - (yScale * (i + 1)) + 7, textPaint);
        }
        //折线
        if (yPoints.size() > 0) {
            for (int i = 1; i < yPoints.size(); i++) {
                canvas.drawLine(xPoint + (i - 1) * xScale, yPoint - yPoints.get(i - 1) * yScale, xPoint + i * xScale, yPoint - yPoints.get(i) * yScale, paint);
            }
        }
    }
}
